package io.github.neo.hhcache.command.string;

import io.github.neo.hhcache.core.Command;
import io.github.neo.hhcache.core.HHCache;
import io.github.neo.hhcache.entry.Reply;

/**
 * @author: hhz
 * @create: 2024/7/10
 **/
public class IncrCommand implements Command {

    @Override
    public String name() {
        return "INCR";
    }

    @Override
    public Reply<?> exec(HHCache cache, String[] args) {
        final String key = getKey(args);
        Reply<?> ret;
        try {
            ret = Reply.integer(cache.incr(key));
        } catch (NumberFormatException nfe) {
            ret = Reply.error("NFE " + key + " value[" + cache.get(key) + "] is not an integer");
        }
        return ret;
    }
}
